package game.MazeRunner;

import java.io.Serializable;

public class player implements Serializable{
	public player() {
		this.cell = new MazeCell();
	}
	public player(MazeGenerator maze) {
		this.x = maze.startingPoint[0];
		this.y = maze.startingPoint[1];
		this.cell = maze.board[x][y];
	}
	
	public boolean setPlayer ( MazeGenerator maze, SmallPlayer sp ) {
		this.x = sp.x;
		this.y = sp.y;
		this.isFreezed = sp.z;
		this.Fcount = sp.f;
		this.cell = maze.board[x][y];
		return true;
	}
	
	public int move(MazeGenerator maze, char direction) {
		System.out.println(cell.next.size());
		int pk=-1;
		if(!isFreezed) {
			if (direction == 'U') {
				for(int i=0; i<cell.next.size(); i++)
					if(cell.next.get(i).y == y-1) {
						y=y-1;  break;
					}
				if (cell.prev!=null && cell.prev.y == y-1)
					y=y-1;
			}
			if (direction == 'D') {
				for(int i=0; i<cell.next.size(); i++)
					if(cell.next.get(i).y == y+1) {
						y=y+1;  break;
				}
				if (cell.prev!=null && cell.prev.y == y+1)
					y=y+1;
			}
			if (direction == 'L') {
				for(int i=0; i<cell.next.size(); i++)
					if(cell.next.get(i).x == x-1) {
						x=x-1;	break;
				}
				if (cell.prev!=null && cell.prev.x == x-1)
					x=x-1;
			}
			if (direction == 'R') {
				for(int i=0; i<cell.next.size(); i++)
					if(cell.next.get(i).x == x+1) {
						x=x+1;	break;
				}
				if (cell.prev!=null && cell.prev.x == x+1)
					x=x+1;
			}
			pk=x*100+y;
			this.cell = maze.board[x][y];
			if(cell.hasTeleport) {
				do {
					x = (int)(Math.random()*(maze.sizeX));
					y = (int)(Math.random()*(maze.sizeY));
				} while(x==maze.endingPoint[0] && y==maze.endingPoint[1]);
				this.cell.hasTeleport = false;
				this.cell = maze.board[x][y];
			}
			if(cell.isFreezed) {
				this.isFreezed = true;
				this.cell.isFreezed = false;
			}
		}
		else {
			if(Fcount == 3 && direction == 'R') {
				isFreezed = false;
				Fcount = 0;
			} else if(Fcount == 3) { Fcount=0; }	
			if(Fcount == 2 && direction == 'D') {
				Fcount++;
			} else if(Fcount == 2) { Fcount=0; }
			if(Fcount == 1 && direction == 'L') {
				Fcount++; 
			} else if(Fcount == 1) { Fcount=0; }	
			if(Fcount == 0 && direction == 'U')
				Fcount++;
		}
		return pk;
	}
	boolean isFreezed = false;
	public MazeCell cell;
	public int x;
	public int y;
	int Fcount = 0;
}
